package com.parkingspace.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.parkingspace.dao.CarMapper;
import com.parkingspace.dao.PersonWalletMapper;
import com.parkingspace.dao.UserMapper;
import com.parkingspace.dao.WalletUserMapper;
import com.parkingspace.entity.Car;
import com.parkingspace.entity.PersonWallet;
import com.parkingspace.entity.User;
import com.parkingspace.entity.WalletUser;
import com.parkingspace.service.IUserService;
import com.parkingspace.util.JwtUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author Gapper
 * @since 2021-05-24
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private CarMapper carMapper;

    @Resource
    private WalletUserMapper walletUserMapper;

    @Resource
    private PersonWalletMapper personWalletMapper;

    @Override
    public Map<String, Object> findUserByUid(Long uid) {
        Map<String,Object> map = new HashMap<>();

        User user = userMapper.selectById(uid);
        map.put("user",user);

        Map<String,Object> carMap = new HashMap<>();
        carMap.put("uid",uid);
        List<Car> cars = carMapper.selectByMap(carMap);
        map.put("carNum",cars.size());


        Map<String,Object> userMap = new HashMap<>();
        userMap.put("uid",uid);
        userMap.put("rid",3);
        List<WalletUser> walletUsers = walletUserMapper.selectByMap(userMap);
        Long walletId = walletUsers.get(0).getWalletId();

        PersonWallet personWallet = personWalletMapper.selectById(walletId);
        map.put("money",personWallet.getMoney());

        return map;
    }

    @Override
    public Boolean updateUser(Long uid, User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid);
        boolean b = userMapper.update(user, queryWrapper) == 1;
        if (!b) {
            throw new RuntimeException("修改失败!");
        }
        return b;
    }
}
